package com.ruoyi.web.controller.kycinfo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.kycinfo.domain.GrantRecord;
import com.ruoyi.kycinfo.domain.KycUserInfo;
import com.ruoyi.kycinfo.service.IGrantRecordService;
import com.ruoyi.kycinfo.service.IKycService;
import com.ruoyi.kycinfo.service.IKycUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 营销宣传品发放记录
 * Controller
 *
 * @author wzk
 * @date 2023-10-02
 */


@Api("参数配置")
@RestController
@RequestMapping("/kycinfo/grant")
public class GrantRecordController extends BaseController {
    @Autowired
    private IGrantRecordService grantRecordService;

    @Autowired
    private IKycService kycService;
    @Autowired
    private IKycUserInfoService kycUserInfoService;

    /**
     * 查询营销宣传品发放记录
     * 列表
     */

    @ApiOperation("查询营销宣传品发放记录")
    @PreAuthorize("@ss.hasPermi('kycinfo:grant:list')")
    @GetMapping("/list")
    public TableDataInfo list(GrantRecord grantRecord) {
        List<GrantRecord> list = new ArrayList<>();
        List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();

        if (userInfoIdList.size() == 0) {
            return getDataTable(list);
        }
        grantRecord.setUserInfoIdList(userInfoIdList);

        startPage();
        list = grantRecordService.selectGrantRecordList(grantRecord);

        return getDataTable(list);
    }

    /**
     * 导出营销宣传品发放记录
     * 列表
     */

    @ApiOperation("导出营销宣传品发放记录")
    @PreAuthorize("@ss.hasPermi('kycinfo:grant:export')")
    @Log(title = "营销宣传品发放记录 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GrantRecord grantRecord) {
        List<GrantRecord> list = grantRecordService.selectGrantRecordList(grantRecord);
        ExcelUtil<GrantRecord> util = new ExcelUtil<GrantRecord>(GrantRecord.class);
        util.exportExcel(response, list, "营销宣传品发放记录数据");
    }

    /**
     * 获取营销宣传品发放记录详细信息
     */

    @ApiOperation("获取营销宣传品发放记录详细信息")
    @PreAuthorize("@ss.hasPermi('kycinfo:grant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(grantRecordService.selectGrantRecordById(id));
    }


    /**
     * 新增营销宣传品发放记录
     */
    @ApiOperation("新增营销宣传品发放记录")
    @PreAuthorize("@ss.hasPermi('kycinfo:grant:add')")
    @Log(title = "营销宣传品发放记录 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GrantRecord grantRecord) {
        // 客户号校验，输入的客户号必须是自己的客户
        Long userInfoId = grantRecord.getClientId();
        List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();
        if (!userInfoIdList.contains(userInfoId)){
            return AjaxResult.error("您没有权限访问该客户信息，请检查客户号");
        }

        KycUserInfo kycUserInfo = kycUserInfoService.selectKycUserInfoByUserInfoId(userInfoId);
        if (kycUserInfo == null) {
            return AjaxResult.error("您输入的客户号信息不存在，请检查客户号");
        }
        if(grantRecord.getClientLevel()==null){
            return AjaxResult.error("请将客户层级信息填写完整！");
        }
        if(grantRecord.getGetType()==null){
            return AjaxResult.error("请将领取礼品品类信息填写完整！");
        }
        return toAjax(grantRecordService.insertGrantRecord(grantRecord));
    }

    /**
     * 修改营销宣传品发放记录
     */

    @ApiOperation("修改营销宣传品发放记录")
    @PreAuthorize("@ss.hasPermi('kycinfo:grant:edit')")
    @Log(title = "营销宣传品发放记录 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GrantRecord grantRecord) {
        return toAjax(grantRecordService.updateGrantRecord(grantRecord));
    }

    /**
     * 删除营销宣传品发放记录
     */
    @ApiOperation("删除营销宣传品发放记录")
    @PreAuthorize("@ss.hasPermi('kycinfo:grant:remove')")
    @Log(title = "营销宣传品发放记录 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(grantRecordService.deleteGrantRecordByIds(ids));
    }
}
